<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  
  <link rel="shortcut icon" href="img/favicon.ico">
  <title>Generic - Java Note</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="css/theme.css" />
  <link rel="stylesheet" href="css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "Generic";
    var mkdocs_page_input_path = "Generic.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="js/jquery-2.1.1.min.js" defer></script>
  <script src="js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
        <a href="." class="icon icon-home"> Java Note</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="index.html">Home</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="StringX.html">StringX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Collection.html">Collection</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="IO.html">IOFile</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Lambda.html">Lambda</a>
                    </li>
                </ul>
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href="Generic.html">Generic</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#method">Method</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#boundedgeneric">BoundedGeneric</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#raw-types">Raw Types</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#wildcard-generic-type">Wildcard Generic Type</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#erasure-and-restriction">Erasure and Restriction</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#restriction">Restriction</a>
    </li>
        </ul>
    </li>
    </ul>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Stream.html">Stream</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Atomic.html">Atomic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Reflect.html">Reflect</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="JavaFX.html">JavaFX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Sundry.html">Sundry</a>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href=".">Java Note</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href=".">Docs</a> &raquo;</li>
    
      
    
    <li>Generic</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h2 id="generic">Generic</h2>
<h3 id="method">Method</h3>
<pre><code class="language-java">public class Demo {
    public static void main(String[]args){
        print(42);
    }
    public static&lt;E&gt; void print(E elem){
        System.out.print(elem);
    }
}
</code></pre>
<h3 id="boundedgeneric">BoundedGeneric</h3>
<pre><code class="language-java">public static&lt;E extends __class__&gt; retType __name(){}
</code></pre>
<h3 id="raw-types">Raw Types</h3>
<p><code>GenericStack stack= new GenericStack();//raw type</code></p>
<p>roughly equivalent to</p>
<p><code>GenericStack&lt;Object&gt; stack= new GenericStack&lt;Object&gt;();</code></p>
<blockquote>
<p>A generic class such as <strong>GenericStack</strong> and <strong>ArrayList</strong> used without a type parameter is called a raw type</p>
<p><strong>raw type</strong> is unsafe which can't check the syntax before runtime</p>
</blockquote>
<h3 id="wildcard-generic-type">Wildcard Generic Type</h3>
<blockquote>
<p>You can use unbounded wildcards, bounded wildcards, or lower bound wildcards to 
  specify a range for a generic type</p>
</blockquote>
<p>formations</p>
<ol>
<li><code>?</code>  unbound wildcard <ol>
<li>is the same as <code>? extends Object</code></li>
</ol>
</li>
<li><code>? extends T</code> bound wildcard<ol>
<li>represents <strong>T</strong> or a subtype of <strong>T</strong></li>
</ol>
</li>
<li><code>? super T</code> a lower bound wildcard<ol>
<li>denotes <strong>T</strong> or a supertype of <strong>T</strong></li>
</ol>
</li>
</ol>
<blockquote>
<p><strong>T</strong> is a generic type</p>
</blockquote>
<pre><code class="language-java">public static void print(GenericStack&lt;?&gt;stack){// unbound wildcard
    ...
}
public static &lt;T&gt; void add(GenericStack&lt;T&gt; stack1,// &lt;? super T&gt;
    GenericStack&lt;? super T&gt; stack2){
    ...
}
</code></pre>
<p><img alt="inheritance" src="Generic/photo/image-20200918095256400.png" /></p>
<h3 id="erasure-and-restriction">Erasure and Restriction</h3>
<blockquote>
<p>The information on generics is used by the compiler but is not available at runtime. 
  This is called <strong>type erasure</strong>.</p>
</blockquote>
<p>deal generic infomation then erase the info </p>
<h4 id="restriction">Restriction</h4>
<ol>
<li><strong>no</strong> <code>new E()</code><ol>
<li>generic-type parameter will be instantiated at <strong>compile time</strong></li>
<li>but <strong>new E()</strong> is executed at <strong>runtime</strong></li>
</ol>
</li>
<li><strong>no</strong> <code>new E[]</code><ol>
<li><code>E[] elements = (E[])new Object[capacity];//circumvent this limitation by casting</code></li>
<li>unchecked compile warning occur <code>ClassCastExcejption</code></li>
</ol>
</li>
<li>generic type parameter of a class is not allowed in a static context<ol>
<li>静态成员不能使用类的泛型参数</li>
</ol>
</li>
</ol>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="Stream.html" class="btn btn-neutral float-right" title="Stream">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="Lambda.html" class="btn btn-neutral" title="Lambda"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="Lambda.html" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="Stream.html" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme.js" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
